﻿#include "application.h"
#include"../errorCheck/errorCheck.h"
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<iostream>
Application* Application::inst = nullptr;
void Application::test() {
	std::cout << "test" << std::endl;
}

Application* Application::getInstance()
{
	if (inst == nullptr) {
		inst = new Application();
	}
	return inst;
}
Application::~Application()
{
}
bool Application::init(uint32_t width,uint32_t height)
{
	mWidth = width;
	mHeight = height;
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	mWindow = glfwCreateWindow(mWidth, mHeight, "OpenGLStudy", NULL, NULL);
	glfwMakeContextCurrent(mWindow);
	// 初始化 GLad
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cerr << "Failed to initialize GLAD" << std::endl;
		return false;
	}
	myapp->test();

	//设置OPenGL视口及其清理颜色,其实就是用这种颜色覆盖画布以达到清理的效果
	glViewport(0, 0, 800, 600);
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//这里的颜色是从0到1的浮点数
	glfwSetWindowUserPointer(mWindow, this);//这里就相当于将this这个指针传到了mWindow这个窗口中存储
	glfwSetFramebufferSizeCallback(mWindow, frameBufferResizeCallBack);
	glfwSetKeyCallback(mWindow, keyCallBack);
	glfwSetMouseButtonCallback(mWindow, mouseButtonCallBack);
	glfwSetCursorPosCallback(mWindow, cursorPosCallBack);
	glfwSetScrollCallback(mWindow, scrollCallBack);
	return true;
}
bool Application::update()
{
	if (glfwWindowShouldClose(mWindow))return false;
	glfwPollEvents();




	//切换双缓存
	glfwSwapBuffers(mWindow);
	return true;
}
void Application::destroy()
{
	glfwTerminate();
}
Application::Application() {
}

void Application::getCursorPos(double* x, double* y)
{
	//获取当前的鼠标位置
	glfwGetCursorPos(mWindow, x, y);
}

void Application::frameBufferResizeCallBack(GLFWwindow* window, int width, int height) {
	std::cout << "frameBufferResizeCallBack" << std::endl;
	Application* self = (Application*)glfwGetWindowUserPointer(window);//获取上面存储的this指针
	if (self->mResizeCallBack != nullptr) {//有可能执行到这里还是空的，所以要判断一下
		self->mResizeCallBack(width, height);
	}

}

void Application::keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods)
{
	std::cout << "keyCallBack" << std::endl;
	Application* self = (Application*)glfwGetWindowUserPointer(window);//获取上面存储的this指针
	if (self->mKeyBoardCallBack != nullptr) {//有可能执行到这里还是空的，所以要判断一下
		self->mKeyBoardCallBack(key, action,mods);
	}
}

void Application::mouseButtonCallBack(GLFWwindow* window, int button, int action, int mods)
{
	std::cout << "鼠标点击：" << button << " " << action << " " << mods << std::endl; 
	Application* self = (Application*)glfwGetWindowUserPointer(window);
	if (self->mMouseButtonCallBack != nullptr) {
		self->mMouseButtonCallBack(button, action, mods);
	}
}

void Application::cursorPosCallBack(GLFWwindow* window, double xpos, double ypos)
{
	std::cout << "鼠标移动:" << xpos << " " << ypos << std::endl;
	Application* self = (Application*)glfwGetWindowUserPointer(window);
	if (self->mCursorPosCallBack != nullptr) {
		self->mCursorPosCallBack(xpos, ypos);
	}
}

void Application::scrollCallBack(GLFWwindow* window, double xoffset,double yoffset)
{
	std::cout << "滚轮滚动" << std::endl;
	Application* self = (Application*)glfwGetWindowUserPointer(window);
	if (self->mScrollCallBack != nullptr) {
		self->mScrollCallBack(yoffset);
	}
}
